package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: dw
 * @Date 2022/3/10 16:50
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    @Autowired(required = false)
    private HttpSession session;

    @Autowired(required = false)
    private DishMapper dishMapper;

    @Autowired(required = false)
    private SetmealMapper setmealMapper;

    /**
     * 分页查询
     * @param page 当前页
     * @param pageSize 每页数据
     * @return
     */
    @Override
    public Page<Category> findByPage(Integer page, Integer pageSize) {
        //设置当前页和每页数据
        PageHelper.startPage(page,pageSize);
        //调用mapper层查询
        List<Category> categoryList = categoryMapper.findAll();
        //封装成PageInfo
        PageInfo<Category> pageInfo = new PageInfo<>(categoryList);
        //封装成page返回
        Page<Category> pageObj = new Page<>();
        pageObj.setRecords(pageInfo.getList());
        pageObj.setTotal(pageInfo.getTotal());

        return pageObj;
    }

    /**
     * 新增菜系
     *
     * @param category 菜系
     * @return
     */
    @Override
    public boolean save(Category category) {
        //判断该菜系名是否存在
        Category categoryName = categoryMapper.findByName(category);
        if (categoryName!=null) {
            throw new BusinessException("该菜系名已存在");
        }
        //获取会话域中的id
        Long id = (Long) session.getAttribute("employee");
        //补全数据
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(id);
        category.setUpdateUser(id);

        return categoryMapper.save(category)>0;
    }

    /**
     * 根据id删除菜系
     *
     * @param id
     * @return
     */
    @Override
    public boolean delete(Long id) {
        //判断该菜系下有无关联菜品
        if (dishMapper.findCountById(id)>0) {
            throw new BusinessException("该菜系关联菜品，无法删除");
        }

        //判断该菜系下有无关联套餐
        if (setmealMapper.findCountBySetMeal(id)>0) {
            throw new BusinessException("该菜系关联套餐，无法删除");
        }


        return categoryMapper.delete(id)>0;
    }

    /**
     * 修改菜系
     *
     * @param category
     * @return
     */
    @Override
    public boolean update(Category category) {
        //判断该菜系名是否存在，排除自己比较
        Category categoryName = categoryMapper.findByName(category);
        if (categoryName!=null && !category.getId().equals(categoryName.getId())) {
            throw new BusinessException("该菜系名字已存在，请重新填写");
        }
        //获取会话域中id
        Long id = (Long) session.getAttribute("employee");
        //补全数据
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(id);
        //调用mapper修改
        return categoryMapper.update(category)>0;
    }

    /**
     * 导出分类信息
     *
     * @return List
     */
    @Override
    public List<Category> findAll() {
        //dao层这个方法可以查所有
        List<Category> list = categoryMapper.findAll();
        //返回所有数据封装成list
        return list;
    }

    /**
     * 根据类别查询
     *
     * @param type
     * @return List
     */
    @Override
    public List<Category> findAllByType(Integer type) {
        List<Category> categoryList = categoryMapper.findAllByType(type);
        return categoryList;
    }
}
